/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.panel;

import com.mysql.jdbc.Connection;
import handler.database;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Room;
import model.Rooms;

/**
 *
 * @author user
 */
public class RoomPanel extends RoomCategory {

    public RoomPanel(int id, String name, Rooms rooms) {
        super(id, name);
        generateData();
        addRooms(rooms);
    }

    private void generateData() {
        try {
            database db;
            Connection conn;
            Statement statement;
            String query;
            ResultSet rs;

            db = new database();
            conn = (Connection) db.getConnection();
            statement = conn.createStatement();
            query = "SELECT * FROM category WHERE parent_id=0";
            rs = statement.executeQuery(query);

            while (rs.next()) {
                subCategories.add(new RoomCategory(Integer.parseInt(rs.getString(1)), rs.getString(2)));
            }

            for (int i = 0; i < subCategories.size(); ++i) {
                RoomCategory rm = subCategories.get(i);
                query = "SELECT * FROM category WHERE parent_id=" + rm.id;
                rs = statement.executeQuery(query);

                while (rs.next()) {
                    rm.subCategories.add(new RoomCategory(Integer.parseInt(rs.getString(1)), rs.getString(2)));
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(RoomPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(RoomPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(RoomPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(RoomPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void addRooms(Rooms rooms) {
        for (int i = 0; i < rooms.size(); ++i) {
            Room room = rooms.get(i);
            RoomCategory rm =find(room.categoryId);
            if (rm != null) {
                rm.rooms.add(room);
            }
        }
    }
}
